package com.example.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.Dao.CommentDao;
import com.Dao.NewsDao;
import com.adapter.NewsAdapter;
import com.news.model.Comment;
import com.news.model.News;

import java.util.ArrayList;
import java.util.List;

public class CommentNewsActivity extends AppCompatActivity {
    private ListView listView;
    private List<News> newsList =  new ArrayList<>();;
    private List<Comment> commentList = new ArrayList<>();
    private CommentDao commentDao;
    private NewsDao newsDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_comment_news);
        SharedPreferences sharedPreferences = getSharedPreferences("user_info",MODE_PRIVATE);
        int userid = sharedPreferences.getInt("userid",-1);
        //初始化
        commentDao  = new CommentDao(this);
        newsDao = new NewsDao(this);
        Log.i("获取用户id",String.valueOf(userid));
        // 查找ListView
        listView = (ListView) findViewById(R.id.commented_view);
        //获取所有评论
        commentList = commentDao.getCommentsByUserid(userid);
        //根据评论的newsid找出对应的新闻
        for(Comment c : commentList){
            Log.i("评论内容",c.getContent());
            News news = newsDao.getNewsByNewsid(c.getNewsId());
            //去重未实现
            if(news!=null)
                newsList.add(news);
        }
        // 创建NewsAdapter
        NewsAdapter newsAdapter = new NewsAdapter(this, newsList);
        // 设置ListView的适配器
        listView.setAdapter(newsAdapter);
        // 设置ListView的点击事件监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News selectedNews = newsList.get(position);
                // 创建Intent，跳转到ArticleLoaderActivity
                Intent intent = new Intent(CommentNewsActivity.this, ArticleActivity.class);
                // 传递新闻ID
                intent.putExtra("newsId", selectedNews.getNewsId());
                // 启动ArticleLoaderActivity
                startActivity(intent);
            }
        });
    }
}